package de.tum.getraenke;

import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.TextView;

public class FavoritesAdapter extends CursorAdapter {

	private LayoutInflater inflator;
	private int cName, cPacking, cSize, cPrice, cDeposit;
	
	public FavoritesAdapter(Context context, Cursor c) {
		super(context, c);
		inflator = LayoutInflater.from(context);
		cName = c.getColumnIndex(DrinkDBHandler.DRINK_NAME);
		cPacking = c.getColumnIndex(DrinkDBHandler.DRINK_PACKING);
		cSize = c.getColumnIndex(DrinkDBHandler.DRINK_SIZE);
		cPrice = c.getColumnIndex(DrinkDBHandler.DRINK_PRICE);
		cDeposit = c.getColumnIndex(DrinkDBHandler.DRINK_DEPOSIT);
	}

	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		String name = cursor.getString(cName);
		String packing = cursor.getString(cPacking);
		float size = cursor.getFloat(cSize);
		float price = cursor.getFloat(cPrice);
		float deposit = cursor.getFloat(cDeposit);
		float total = (float) Math.round((price + deposit) * 100) / 100;
		TextView productName = (TextView) view.findViewById(R.id.productName);
		TextView productPacking = (TextView) view.findViewById(R.id.productPacking);
		TextView productPrice = (TextView) view.findViewById(R.id.productPrice);
		TextView productDeposit = (TextView) view.findViewById(R.id.productDeposit);
		productName.setText(name + (size > 0.0 ? " " + String.valueOf(size) + "l" : ""));
		productPacking.setText(packing);
		productPrice.setText(String.valueOf(total) + "€");
		productDeposit.setText("Deposit: " + String.valueOf(deposit) + "€");
	}

	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		return inflator.inflate(R.layout.icon_text_text, null);
	}
}
